package com.hnas.trade.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.hnas.common.service.dto.search.DTOOrderSearch;
import com.hnas.core.dao.BaseDaoImpl;
import com.hnas.core.dao.PageBean;
import com.hnas.core.dto.DTOPagedList;
import com.hnas.core.util.StringUtil;
import com.hnas.trade.dao.OrderDetailDao;
import com.hnas.trade.orm.OrderDetail;
import com.hnas.trade.service.dto.DTOOrderDetail;
import com.hnas.trade.service.mapper.OrderDetailMapper;

@Component("orderDetailDao")
public class OrderDetailDaoImpl extends BaseDaoImpl<OrderDetail> implements OrderDetailDao{

	public OrderDetailDaoImpl() {
		super(OrderDetail.class);
	}

	@Override
	public List<OrderDetail> findByOrderNo(String orderNo) {
		return this.findByQuery("from OrderDetail t where t.orderNo=?1", orderNo);
	}

	@Override
	public OrderDetail saveOrUpdate(DTOOrderDetail dto) throws Exception {
		OrderDetail vo = null;
		if(dto != null){
			String id = dto.getId();
			if(StringUtil.isNotEmpty(id)){
				vo = this.find(id);
			}
			if(vo == null){//新增
				vo = new OrderDetail();
				BeanUtils.copyProperties(dto, vo);
				this.persist(vo);
			}else{//修改
				BeanUtils.copyProperties(dto, vo, new String[]{"createTime","creator"});
				this.merge(vo);
			}
		}
		return vo;
	}

	@Override
	public OrderDetail findByOrderNoAndItemId(String orderNo, String itemId) {
		String hql = "from OrderDetail t where t.orderNo=?1 and t.relateId = ?2";
		List<OrderDetail> findByQuery = this.findByQuery(hql, orderNo,itemId);
		if(findByQuery == null || findByQuery.size() == 0) return null;
		return findByQuery.get(0);
	}

	@Override
	public DTOPagedList<DTOOrderDetail> search(DTOOrderSearch criteria) {
		DTOPagedList<DTOOrderDetail> pageList = new DTOPagedList<DTOOrderDetail>();
		List<DTOOrderDetail> list = new ArrayList<DTOOrderDetail>();
		PageBean<OrderDetail> page = new PageBean<OrderDetail>();
		page.setPageNo(criteria.getPageNo());
		page.setPageSize(criteria.getPageSize());
		page.setTotalCount(criteria.getTotalCount());
		StringBuffer sb = new StringBuffer();
		List<Object> paras = new ArrayList<Object>();
		int j = 1;
		if(StringUtil.isNotEmpty(criteria.getOrderNo())){
			sb.append(" and t.orderNo = ?"+j++);
			paras.add(criteria.getOrderNo().trim());
		}
		
		if(StringUtil.isNotEmpty(criteria.getStartDate())){
			sb.append(" and t.createTime >= ?"+j++);
			paras.add(criteria.getStartDate().trim()+" 00:00:00");
		}
		
		if(StringUtil.isNotEmpty(criteria.getEndDate())){
			sb.append(" and t.createTime <= ?"+j++);
			paras.add(criteria.getEndDate().trim()+" 23:59:59");
		}
		
		if(sb.length() > 0){
			sb.delete(0, 5);
			sb.insert(0, " from OrderDetail t  where ");
		}else{
			sb.append(" from OrderDetail t ");
		}
		sb.append(" order by t.createTime desc");
		page.setQuery(sb.toString());
		page.setQueryCount("select count(t.id) "+sb);
		this.queryByPage(page, paras.toArray());
		pageList.setPageNo(page.getPageNo());
		pageList.setPageSize(page.getPageSize());
		pageList.setTotalCount(page.getTotalCount());
		for(OrderDetail vo : page.getList()){
			DTOOrderDetail dto = new DTOOrderDetail();
			dto = OrderDetailMapper.getInstance().mapToValue(vo);
			list.add(dto);
		}
		pageList.setList(list);
		return pageList;
	}

}
